<?php
namespace app\admin\controller;

use think\Controller;

use app\admin\model\User as UserModel;
use app\admin\model\Deallist as DLModel;
use app\admin\model\Businesscenter as BCModel;
use think\Session;

class Business extends  AdminBase{

    //用户登录展示页面
    public function index(){


        //实例化模型
        $BCmodel = new BCModel();

        $data = $BCmodel->order('Business_level')->select();

        $this->assign('data',$data);

        return $this->fetch();
    }

    //报单申请页面展示
    public function business_apply(){

        $Info = input('param.');


       
        //dump($UserInfo);die;
//        $CenterID = ($UserInfo['centerid'] > 0 )? $UserInfo['centerid'] : 0;
        //$CenterID = ($Info['centerid'] != 0) ? $Info['centerid'] : 0;
        $CenterID = ($Info) ? $Info['centerid'] : 0;
        //dump($UserInfo);die;

        $this->assign('centerid',$CenterID);

    	return $this->fetch();

    }

    //报单申请页面表单
    public function business_applyForm(){


        $Bussiness_applyInfo = input('post.');

//        dump($Bussiness_applyInfo);die;

/*        array(3) {
            ["user_id"] => string(2) "40"
            ["list_money"] => string(4) "2222"
            ["business_center_num"] => string(1) "0"
}*/

        //暂时不用
        $ListDes = $Bussiness_applyInfo['list_des'] ? $Bussiness_applyInfo['list_des'] : '0';

        //存入数据库

        $dlmodel = new DLModel();

        $dlmodel->data([

            'User_id' => $Bussiness_applyInfo['user_id'],
            'deal_price' => $Bussiness_applyInfo['list_money'],
            'Business_Center_id' => $Bussiness_applyInfo['business_center_num'],
            'description' => $ListDes,
            'tree_status' => $Bussiness_applyInfo['fork']
        ]);
        $Res = $dlmodel->save();

            //dump($Res);die;
        if($Res){
            //保存成功
            $this->redirect('Come/index');
        }else{
             $this->error('修改失败');
        }


    }

//    public function b(){
//        return $this->fetch();
//    }


    //报单 账单展示

    public function business_list(){

       

        //实例化模型

        $dlmodel = new DLModel();

        $data = $dlmodel->paginate(10);

        //dump($data);die;
        $page = $data->render();
        $total = $data->total();
        $count = $data->count();

        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('total',$total);
        $this->assign('count',$count);



        return $this->fetch();
    }
    //用户列表页面
    public function businesscenter(){
       
        $Businesscenter = new BCModel();
        
        //$data = $payList->where('userid', $UserID)->select();
        $data = $Businesscenter->paginate(10);
         //dump($data);die;
        
        $this->assign('data',$data);
        
         $page = $data->render();
         $total = $data->total();
         $count = $data->count();

        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('total',$total);
        $this->assign('count',$count);
        return $this->fetch();
    }
    public function modifyBusiness(){ 

         $AllData = input('post.');
       
       //$AllData['str'] = '1,22,hao1,2,1,6,2018-04-12 16:41:15';

        // $AllData['userinfo_str']
        //$a = $a['action'];
        //$a = $a['username'];
        //$c = "action =".$a.",username = ".$b;
        // file_put_contents("cc.txt",$AllData['str'],FILE_APPEND);die;
        //echo $a;
        $BCModel = new BCModel();
        $BusinessModifyList = explode(',', $AllData['str']);
        //$BusinessModifyList = explode(',', $AllData);
        //dump($BusinessModifyList);die;        
        $SuccessArray = [];
        $FailArray = [];
        //看看存在吗
        if($BCModel->where(['id' => $BusinessModifyList['0']])->find()){
                //存在就更新
                $Res = $BCModel->save(
                        [
                    'user_name'  => $BusinessModifyList['1'],
                    //'create_time'  => $BusinessModifyList['2'],
                    'all_pay' => $BusinessModifyList['3'],
                    'Business_level' => $BusinessModifyList['4'],
                    'Business_percent' => $BusinessModifyList['5']],                
                                ['id' => $BusinessModifyList['0']]);
                        //给出成功标志
                        $SuccessArray[] = 1;
                if($Res){
                   // echo "111";
                }else{
                     // echo '222';
                }
        }else{
                //不存在直接返回
                $FailArray []= 1;
            echo  'false';
        }die;
        echo $result['id'];die;
        //返回的数组
        $StatusArray = [];
        $StatusArray['status'] = "1";
        $StatusArray['success'] = $SuccessArray ? $SuccessArray : NULL;
        //$StatusArray['success'] = ['aa','bb'];
        $StatusArray['fail'] = $FailArray ? $FailArray : NULL;

        // dump($StatusArray);die;
        //var_dump($StatusArray);die;
        return json_encode($StatusArray);
    }

    //删除数据函数 ajax
    public function deleteBusiness(){

        $AllData = input('post.');
        //$AllData = '3,hao,2018-03-29 17:01:27,3333333,0,11111111,0';

        $BCModel = new BCModel();
       
        //从$data中得到名字字符串，并且转成数组
         $BusinessDeleteList = explode(',', $AllData['userinfo_str']);
         //dump($UserNameList);
         
         $SuccessArray = [];
         $FailArray = [];

        foreach ($BusinessDeleteList as $key => $value) {
            if($BCModel->where(['id' => $value])->find()){
                //从数据库能找到,就删除该条数据
                if($BCModel->where(['id' => $value])->delete()){

                    $SuccessArray[]= $value;
                    //echo "删除成功";
                }else{
                    $FailArray[]= $value;
                    //echo "删除失败";
                }
            }else{
                $FailArray[]= $value;
                //echo "没有本条数据";
            }
        }

        //返回的数组
        $StatusArray = [];
        $StatusArray['status'] = "1";
        $StatusArray['success'] = $SuccessArray ? $SuccessArray : NULL;
        //$StatusArray['success'] = ['aa','bb'];
        $StatusArray['fail'] = $FailArray ? $FailArray : NULL;

        //var_dump($StatusArray);die;
        return json_encode($StatusArray);
        
    }

    //未审核账单 审批
    public function unaudited(){
       
        //实例化模型

        $dlmodel = new DLModel();

        $data = $dlmodel->where('status',0)->paginate(10);

        //dump($data);die;
        $page = $data->render();
        $total = $data->total();
        $count = $data->count();

        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('total',$total);
        $this->assign('count',$count);

        return $this->fetch();
    }
    //未审核账单 审批 同意
    public function unauditedAdopt(){
       
        $withDrawInfo = input('get.');
        //dump($withDrawInfo);
        //file_put_contents('1.txt',$withDrawInfo['withDraw_id'],FILE_APPEND);
        $PaylistModel = new DLModel();
        $result = $PaylistModel->save(['status'  => '1'],['id' => $withDrawInfo['withDraw_id']]);
    }
    //未审核账单 审批 拒绝
    public function unauditedRefuse(){
       $withDrawInfo = input('get.');
        //dump($withDrawInfo);
        // file_put_contents('1.txt',$withDrawInfo['withDraw_id'],FILE_APPEND);
        $PaylistModel = new DLModel();
        $result = $PaylistModel->save(['status'  => '2'],['id' => $withDrawInfo['withDraw_id']]);
    }






    //未审核报单中心 审批
    public function unauditedCenter(){
       

        //实例化模型

        $bcmodel = new BCModel();

        $data = $bcmodel->where('status',0)->paginate(10);

        //dump($data);die;
        $page = $data->render();
        $total = $data->total();
        $count = $data->count();

        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('total',$total);
        $this->assign('count',$count);

        return $this->fetch();
    }
    //未审核报单中心 审批 同意
    public function unauditedAdoptCenter(){

        $withDrawInfo = input('get.');
        //dump($withDrawInfo);
         //file_put_contents('1.txt',$withDrawInfo['withDraw_id'],FILE_APPEND);
        $bcmodel = new BCModel();
        $result = $bcmodel->save(['status'  => '1'],['id' => $withDrawInfo['withDraw_id']]);
    }
    //未审核报单中心 审批 拒绝
    public function unauditedRefuseCenter(){
        $withDrawInfo = input('get.');
        //dump($withDrawInfo);
        // file_put_contents('1.txt',$withDrawInfo['withDraw_id'],FILE_APPEND);
        $bcmodel = new BCModel();
        $result = $bcmodel->save(['status'  => '2'],['id' => $withDrawInfo['withDraw_id']]);
    }
}
